package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.util.List;
import java.util.Random;

import com.ruoyi.system.domain.Contract;
import com.ruoyi.system.domain.Education;
import com.ruoyi.system.domain.Workinfo;
import com.ruoyi.system.mapper.ContractMapper;
import com.ruoyi.system.mapper.EducationMapper;
import com.ruoyi.system.mapper.WorkinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EmplyeeMapper;
import com.ruoyi.system.domain.Emplyee;
import com.ruoyi.system.service.IEmplyeeService;

/**
 * 合同工Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-13
 */
@Service
public class EmplyeeServiceImpl implements IEmplyeeService
{
    @Autowired
    private EmplyeeMapper emplyeeMapper;
    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private EducationMapper educationMapper;
    @Autowired
    private WorkinfoMapper workinfoMapper;

    /**
     * 查询合同工
     *
     * @param eno 合同工主键
     * @return 合同工
     */
    @Override
    public Emplyee selectEmplyeeByEno(String eno)
    {
        //合同赋值
        Emplyee emplyee = emplyeeMapper.selectEmplyeeByEno(eno);
        Contract contract = contractMapper.selectContractByCno(emplyee.getCno());
        emplyee.setContract(contract);
        //教育经历赋值
        Education edu = educationMapper.selectEducationById(emplyee.getEduId());
        emplyee.setEducat(edu);
        //工作信息赋值
        Workinfo workinfo = workinfoMapper.selectWorkinfoById(emplyee.getWorkId());
        emplyee.setWorkinfo(workinfo);
        return emplyee;
    }

    @Override
    public List<String> selectEmplyeeNames() {
        return emplyeeMapper.selectEmplyeeNames();
    }

    /**
     * 查询合同工列表
     *
     * @param emplyee 合同工
     * @return 合同工
     */
    @Override
    public List<Emplyee> selectEmplyeeList(Emplyee emplyee)
    {
        return emplyeeMapper.selectEmplyeeList(emplyee);
    }

    //根据员工姓名查询员工
    @Override
    public Emplyee selectEmplyeeByName(String name) {
        return emplyeeMapper.selectEmplyeeByName(name);
    }


    /**
     * 新增合同工
     *
     * @param emplyee 合同工
     * @return 结果
     */
    @Override
    public int insertEmplyee(Emplyee emplyee)
    {
        LocalDate today = LocalDate.now();
        Random random = new Random();
        String year = today.getYear()+"";
        year = year.substring(year.length() - 2);
        int month = today.getMonthValue();
        int day = today.getDayOfMonth();
        int rand = 1000 + random.nextInt(9000);
        System.out.println(year+","+month+","+day);
        String eno = "TJISE"+year+month+day+rand;
        emplyee.setEno(eno);
        return emplyeeMapper.insertEmplyee(emplyee);
    }

    /**
     * 修改合同工
     *
     * @param emplyee 合同工
     * @return 结果
     */
    @Override
    public int updateEmplyee(Emplyee emplyee)
    {
        return emplyeeMapper.updateEmplyee(emplyee);
    }

    /**
     * 批量删除合同工
     *
     * @param enos 需要删除的合同工主键
     * @return 结果
     */
    @Override
    public int deleteEmplyeeByEnos(String[] enos)
    {
        return emplyeeMapper.deleteEmplyeeByEnos(enos);
    }

    /**
     * 删除合同工信息
     *
     * @param eno 合同工主键
     * @return 结果
     */
    @Override
    public int deleteEmplyeeByEno(String eno)
    {
        return emplyeeMapper.deleteEmplyeeByEno(eno);
    }

    @Override
    public int getUserNum() {
        return emplyeeMapper.getUserNum();
    }
}
